package com.server.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.server.service.ICommonDataService;
import com.server.service.INoticeService;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("NoticeService")
public class NoticeService implements INoticeService {
	
	private final String TABLE_NAME = "reward.notice";
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ICommonDataService commonDataService;
	
	@Override
	public Boolean add(String operator, Map<String, Object> inputMap){
		return commonDataService.add(operator, TABLE_NAME, inputMap);
	}
	
	@Override
	public DataVector<Map<String, Object>> getList(RequestInput requestInput){
		return commonDataService.getListData(TABLE_NAME, null, "CreateTime desc", requestInput);
	}
	
	@Override
	public Boolean delete(String ids){
		String sql = "delete from %s where id in(%s)";
		return jdbcTemplate.update(String.format(sql, TABLE_NAME,ids)) > 0;
	}
	
	@Override
	public Map<String, Object> getForm(String id){
		return commonDataService.getFormData(TABLE_NAME, "Id", id, null, null);
	}
	
	@Override
	public Boolean update(String operator, Map<String, Object> inputMap){
		Object idObj = inputMap.get("Id");
		if (idObj == null) {
			return false;
		}
		return commonDataService.update(operator, TABLE_NAME, "Id", idObj.toString(), inputMap);
	}
	
	@Override
	public Map<String, Object> getNotice(){
		String sql = "select * from %s where Valid=1 order by Id desc LIMIT 1";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, TABLE_NAME));
		if (list.size() > 0) {
			return list.get(0);
		}else{
			return new HashMap<String, Object>();
		}
	}
	

}
